package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemdictionaryService;
    @RequiresPermissions(value={"字典目录列表","systemDictionary:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute QueryObject qo, Model model) {
        model.addAttribute("pageInfo", systemdictionaryService.query(qo));
        return "systemdictionary/list";
    }
    @RequiresPermissions(value={"字典目录编辑","systemDictionary:input"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", systemdictionaryService.get(id));
        }
        return "systemDictionary/input";
    }

    @RequiresPermissions(value={"字典目录保存或更新","systemDictionary:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary entity) {

        JsonResult result = new JsonResult();
        try {
            //int a=1/0;
            systemdictionaryService.saveOrUpdate(entity);
        }catch (Exception e)
        {
//            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }
    @RequiresPermissions(value={"字典目录删除","systemDictionary:delete"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {

            systemdictionaryService.delete(id);
        }catch (Exception e)
        {
            result.mark("亲,执行失败!");
        }
        return result;
    }
}
